<?php
namespace common\models\info;

use backend\component\ResultFormat;
use common\component\BaseActiveRecord;
use common\component\Constant;
use common\utils\CommonUtil;
use Yii;
use yii\behaviors\TimestampBehavior;
class Photo extends BaseActiveRecord
{

    public static function tableName()
    {
        return '{{zt_photo}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['created_at','album_id'], 'integer'],
            [['abstr','url'], 'string'],
            [['title'], 'string', 'max' => 255,"message"=>"标题超出指定长度"],
        ];
    }
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'abstr' => '简介',
            'created_at' => '创建时间',
            'url'=>'图片',
            'album_id'=>'相册id'
        ];

    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    self::EVENT_BEFORE_INSERT => ['created_at'],
                    self::EVENT_AFTER_UPDATE=>['created_at']
                ]
            ]
        ];
    }

    /**
     * 实例化模型对像
     * @param $id
     * @return BaseActiveRecord|null|static
     */
    public static function modelById($id) {
        if (!$id) {
            $model = new self();
        }
        else {
            $model = static::findOne($id);
        }
        return $model;
    }

    /***
     * 多张  存储图片
     */
    public static function savePhoto($result,$id){
        foreach ($result as $value) {
            $model = new self();
            $params['url'] = $value;
            $params['album_id'] = $id;
            $model->setAttributes($params);
            if (!$model->save()) {
                return ResultFormat::error($model->getErrorsToString());
            }
        }
        return true;
    }
    public static function getAllList($post,$page=1,$pageSize=10){
        $where[] = ['=','album_id',$post['id']];
        $q = static::find()->where(['album_id'=>$post['id']])->orderBy("id DESC");
        $data = static::arrListByPage($q,$page,$pageSize);
        if (count($data["rows"])) {
            foreach ($data["rows"] as $key=>$val) {
                $data["rows"][$key]["link"] = CommonUtil::generateUrl($val['id'], Constant::ROUTE_PHOTO);
                $data["rows"][$key]["created_at"] = date("Y-m-d",$val["created_at"]);
            }
        }
        return $data;
    }
    public static function removePhoto($ids){
        $where = ["and"];
        $where[] = ['in','id',$ids];
        $model = static::find()->where($where)
            ->one();
        if (!$model) {
            return false;
        }
        if (!$model->delete()) {
            return ResultFormat::error($model->getErrorsToString());
        }
       return true;
    }
    public  static function updatePhoto($ids,$id){
        $where = ["and"];
        $where[] = ['in','id',$ids];
        $model = static::find()->where($where)->one();

        $model->album_id =$id;
        if (!$model->update()) {
            return ResultFormat::error($model->getErrorsToString());
        }
        return ResultFormat::success("操作成功");
    }
}
